package com.app.controller.article;

import com.app.aop.has.HasApiLimit;
import com.app.aop.has.HasRedissonLock;
import com.app.aop.has.HasRole;
import com.app.domain.bo.article.ArticleCommentBo;
import com.app.domain.page.Page;
import com.app.domain.vo.article.ArticleCommentVo;
import com.app.framework.common.R;
import com.app.service.article.ArticleCommentService;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 评论控制器
 */
@RestController
@RequestMapping("/api/comment")
public class ArticleCommentController {

    @Resource
    private ArticleCommentService articlecommentService;

    /**
     * 评论列表
     *
     * @param bo 入参
     */
    @PostMapping("/all")
    public R<Page<ArticleCommentVo>> allComment(@RequestBody ArticleCommentBo bo) {
        return R.ok(articlecommentService.allComment(bo));
    }

    /**
     * 用户评论列表
     *
     * @param bo 入参
     */
    @PostMapping("/user")
    public R<Page<ArticleCommentVo>> userComment(@RequestBody ArticleCommentBo bo) {
        Assert.notNull(bo.getUserId(), "用户ID不能为空");
        return R.ok(articlecommentService.userComment(bo));
    }

    /**
     * 评论列表
     *
     * @param bo 入参
     */
    @PostMapping("/list")
    public R<Page<ArticleCommentVo>> lists(@RequestBody ArticleCommentBo bo) {
        Assert.notNull(bo.getArticleId(), "文章ID不能为空");
        return R.ok(articlecommentService.lists(bo));
    }

    /**
     * 查看更多
     *
     * @param bo 入参
     */
    @PostMapping("/mores")
    public R<Page<ArticleCommentVo>> mores(@RequestBody ArticleCommentBo bo) {
        Assert.notNull(bo.getMid(), "主评论ID不能为空");
        Assert.notNull(bo.getArticleId(), "文章ID不能为空");
        return R.ok(articlecommentService.mores(bo));
    }

    /**
     * 添加评论
     *
     * @param bo 入参
     */
    @HasApiLimit(seconds = 2)
    @HasRedissonLock("save_comment")
    @PostMapping("/save")
    public R<?> saveOne(@RequestBody ArticleCommentBo bo) {
        Assert.notNull(bo.getArticleId(), "文章ID不能为空");
        articlecommentService.saveOne(bo);
        return R.ok(null, "评论成功");
    }

    /**
     * 删除评论
     *
     * @param commentId 评论ID
     */
    @HasRole()
    @HasRedissonLock("delete_comment")
    @PostMapping("/delete/{commentId}")
    public R<?> deleteOne(@PathVariable("commentId") Long commentId) {
        articlecommentService.deleteOne(commentId);
        return R.ok(null, "删除成功");
    }

    /**
     * 屏蔽评论
     *
     * @param commentId 评论ID
     */
    @HasRole()
    @PostMapping("/shield/{commentId}")
    public R<?> shield(@PathVariable("commentId") Long commentId) {
        articlecommentService.shield(commentId);
        return R.ok(null, "屏蔽成功");
    }

    /**
     * 评论点赞
     *
     * @param commentId 评论ID
     */
    @HasApiLimit(seconds = 2)
    @HasRedissonLock("like_comment")
    @PostMapping("/like/{commentId}")
    public R<?> like(@PathVariable("commentId") Long commentId) {
        articlecommentService.like(commentId);
        return R.ok(null, "点赞成功");
    }

}